package com.huangguolin.cloud.login.service.impl;


import com.huangguolin.cloud.login.entity.MenuLevelEntity;
import com.huangguolin.cloud.login.entity.loginUserEntity;
import com.huangguolin.cloud.login.repository.LoginDao;
import com.huangguolin.cloud.login.service.LoginService;
import com.huangguolin.cloud.mybatis.entity.PtMenuEntity;
import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import com.huangguolin.cloud.spring.util.MailUtil;
import com.huangguolin.cloud.spring.util.VerifyUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {


    @Resource
    private LoginDao loginDao;

    @Resource
    private HttpSession session;

    @Resource
    private PasswordEncoder passwordEncoder;


    @Override
    public List<PtUserEntity> queryUserList() {
        return loginDao.queryUserList();
    }

    @Override
    public void deleteUserById(String userIds) {
            String[] userIdArray = userIds.split(",");
            for(String userId: userIdArray) {
                loginDao.deleteUserById(userId);
            }
    }

    @Override
    public void resttingPassword(String password1) {
        String password2 = passwordEncoder.encode(password1);
        String Userids = (String) session.getAttribute("Userids");
        loginDao.resttingPassword(password2,Userids);
    }

    @Override
    public PtUserEntity queryUserInfoByName(String userName) {
        return loginDao.queryUserInfoByName(userName);
    }

    @Override
    public List<MenuLevelEntity> queryMenuList() {

        String lang = LocaleContextHolder.getLocale().toString();
        loginUserEntity loginUserEntity = (loginUserEntity)SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        String userUuid = loginUserEntity.getUserUuid();

        List<PtMenuEntity> list = loginDao.queryMenuList(userUuid,lang);


        List<MenuLevelEntity> levelMenuList = new ArrayList<>();

        MenuLevelEntity menuLevelEntity = null;
        for(PtMenuEntity ptMenuEntity : list){
            menuLevelEntity = new MenuLevelEntity(ptMenuEntity);
            levelMenuList.add(menuLevelEntity);

        }

        List<MenuLevelEntity> levelMenuRst = new ArrayList<>();
        Integer index = -1;
        for (MenuLevelEntity item:levelMenuList){
            index = -1;
            for (MenuLevelEntity parent:levelMenuList){
                if(parent.getMenuId().equals(item.getParentId())){
                    index = 0;
                    parent.addChildren(item);
                    break;
                }
            }
            if(index == -1){
                levelMenuRst.add(item);
            }

        }


        return levelMenuRst;
    }

    @Override
    public List<String> queryAuthListById(String userUuid) {

        return loginDao.queryAuthListById(userUuid);
    }

    @Override
    public ResponseEntity<byte[]> getVerifyCodeImg() {//获取验证码
        String verifyCode = VerifyUtil.getVfcCode(4);
        session.setAttribute("verifyCode",verifyCode);
        byte[] imByte = VerifyUtil.getVfcImg(verifyCode);
        HttpHeaders headers =
                new HttpHeaders();
        headers.setContentDispositionFormData("attachment",
                "aaa.jpg");//告知浏览器以下载方式打开
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置 MIME 类型
        return new ResponseEntity<byte[]>(
                imByte,
                headers,
                HttpStatus.CREATED);

    }

    /**
     * 发送邮箱验证码
     * @param emailCode 用户邮箱账号
     */
    @Override
    public void getemailcode(String emailCode) {

       String code = MailUtil.getEmailcode(emailCode);
       session.setAttribute("code",code);//邮箱验证码
    }

    /**
     * 通过邮箱查找用户ID
     * @param emailCode
     */
    @Override
    public boolean queryUserIdByEmail(String emailCode) {
        String Userids = loginDao.queryUserIdByEmail(emailCode);
        session.setAttribute("Userids",Userids);//邮箱验证码
        if(Userids == null){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 注册新用户
     * @param password2
     * @param emailCode
     */
    @Override
    public void registerUser(String password2, String emailCode ,String regName,String regniceName) {
        String password3 = passwordEncoder.encode(password2);
        loginDao.registerUser(password3,emailCode,regName,regniceName);
    }

    /**
     * 校验用户名
     * @param regName
     */
    @Override
    public boolean infoUserName(String regName) {
        String userName = loginDao.infoUserName(regName);
        if(userName == null){
            return false;
        }else{
            return true;
        }

    }


    public ResponseEntity<byte[]> set() {
        String verifyCode = VerifyUtil.getVfcCode(4);
        session.setAttribute("verifyCode",verifyCode);
        byte[] imByte = VerifyUtil.getVfcImg(verifyCode);
        HttpHeaders headers =
                new HttpHeaders();
        headers.setContentDispositionFormData("attachment",
                "aaa.jpg");//告知浏览器以下载方式打开
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);//设置 MIME 类型
        return new ResponseEntity<byte[]>(
                imByte,
                headers,
                HttpStatus.CREATED);

    }
}

